<?php
namespace app\index\controller;
use think\Controller;
use think\Validate;
use think\Db;
use think\Session;
use think\paginate;
use think\captcha;
use think\helper;
use think\Loader;



class Index extends Controller
{
    public function index()
    {
        $images=Db::name('image_big')->select();
        $this->assign('images',$images);
        $report=Db::name('report')->select();
        $this->assign('report',$report);
        $blog=Db::name('blog')->select();
        $this->assign('blog',$blog);
        $talk=Db::name('talk')->order('id desc')->limit(4)->select();
        $this->assign('talk',$talk);
        $blog_count=Db::name('blog')->count();
        $this->assign('blog_count',$blog_count);
        return $this->fetch('/index');
    }

        //-------------------------博客详情页信息----------------------------------
    public function detail($id)
    {
            $id=input('param.id');
            $blog=Db::name('blog')->where('id',$id)->find();
            $this->assign('blog',$blog); 
            $talk=Db::name('talk')->where('blog_id',$id)->select();
            $this->assign('talk',$talk); 
            $foot=Db::name('blog')->order('id desc')->limit(3)->select();
            $this->assign('foot',$foot); 
            return $this->fetch('/detail');
    }

    public function like($id)
    {
        $id=$_GET['id'];
        $blog=Db::name('blog')->where('id',$id)->find();
        $like=$blog['like'];
        $like=$like+1;
        $data=[
            'id'=>$id,
            'like'=> $like
        ];
        Db::table('blog')->update($data);
        $this->redirect("index/detail",['id'=>$id]);
    }
    
    public function talk($id)
    {
        $id=$_GET['id'];
        $blog=Db::name('blog')->where('id',$id)->find();
        $talk=$blog['talk'];
        $talk=$talk+1;
        $data=[
            'id'=>$id,
            'talk'=> $talk
        ];
        Db::table('blog')->update($data);

        $name=$_POST['name'];
        $content=$_POST['talk'];

        $data=[
            'blog_id'=>$id,
            'content'=>$content,
            'name'=>$name,
            'create_time'=>time()

        ];
        Db::table('talk')->insertGetId($data);
        $this->redirect("index/detail",['id'=>$id]);
    }

}
